package com.rzcode.coinex.kernel.common.response;

import com.rzcode.coinex.kernel.common.response.constant.HttpState;
import com.rzcode.coinex.kernel.common.response.utils.MessageUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * 返回前端统一响应
 */
@ApiModel("统一响应")
@Getter
@Setter
public class R<T> {

    @ApiModelProperty("状态码")
    private int code;

    @ApiModelProperty("提示消息")
    private String message;

    @ApiModelProperty("数据")
    private T data;

    public static R ok() {
        return ok(null);
    }

    public static <T> R ok(T data) {
        return ok(HttpState.ok, MessageUtils.message("operate.ok"), data);
    }

    public static <T> R ok(String message, T data) {
        return ok(HttpState.ok, message, data);
    }

    public static <T> R ok(int code, String message, T data) {
        return new R(code, message, data);
    }

    public static R fail(String message) {
        return fail(HttpState.FAIL, message);
    }

    public static R fail(int code) {
        return fail(code, MessageUtils.message("operate.fail"));
    }

    public static R fail(int code, String message) {
        return fail(code, message, null);
    }

    public static <T> R fail(int code, String message, T data) {
        return new R(code, message, data);
    }

    public R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
